package eva

import (
	"context"
	"trusteva/app/eva/model"

	"trusteva/app/eva/internal/svc"
	"trusteva/app/eva/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteWhiteListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteWhiteListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteWhiteListLogic {
	return &DeleteWhiteListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteWhiteListLogic) DeleteWhiteList(req *types.DeleteWhiteListRo) (resp *types.DeleteWhiteListRsp, err error) {
	var m model.Whitelist
	err = l.svcCtx.DB.WithContext(l.ctx).Where("Id=?", req.Id).First(&m).Error
	if err != nil {
		return nil, err
	}
	err = l.svcCtx.DB.WithContext(l.ctx).Where("Id=?", req.Id).
		Delete(&model.Whitelist{}).Error
	if err != nil {
		return nil, err
	}
	_, err = l.svcCtx.RedisClient.DelCtx(l.ctx, m.Key())
	if err != nil {
		return nil, err
	}
	return
}
